(function() {
    angular.module('app').component('chartDashPie', {
        css: '',
        templateUrl: 'app/controllers/industry.controller/chart.dash.pie.component/chart.dash.pie.component.html',
        controller: chartDashPie,
        controllerAs: 'vm',
        bindings: {
            catName: '<'
        }

    });
    chartDashPie.$inject = ['util', 'dashPieService', '$rootScope', '$scope'];

    function chartDashPie(util, dashPieService, $rootScope, $scope) {
        var vm = this;

        vm.chart = {
            isLeaf:false,
            toolBarSeen: false,
            root_catName: '',
            title: '345',
            type: 'pie',
            switchChartType: changeType
        }

        vm.back = back; //返回上一级 类目
        vm.backToTop = backToTop; //返回 top 类目
        vm.getHTTP = getHTTP; //dashPieService function
  
        vm.$onInit = function() {
            // console.log('-------------------catName:');
            // console.log(vm.catName);
            vm.chart.root_catName = vm.catName; //设置catName的根类目
            titleFormat(); //格式化标题

            vm.getHTTP();
        }


        //脏检查 vm.catName  是否展示 toolbar
        $scope.$watch('vm.catName', function(newValue, oldValue) {

            if (newValue == vm.chart.root_catName) {
                vm.chart.toolBarSeen = false;
            } else {
                vm.chart.toolBarSeen = true;
            }
        });

        //nav component 多行业切换广播catName
        var clearFun = $rootScope.$on('industry_cat_EV',function(e,index){

            // console.log('-----------------industry_cat_EV--------------------------');

            var arry = $rootScope.userInfo.itemLevel1Category.split(';');

            vm.catName = arry[index];
            vm.chart.root_catName = arry[index];
            vm.getHTTP();
        });

        $scope.$on('$destroy',function(){
            clearFun();
        });


        // 类目占比饼图
        vm.option_for_pie = {
            tooltip: {
                trigger: 'item',
                formatter: function(params) {
                    // console.log(params);
                    var str = '<div style = "text-align: center;">' + params.name + '</div>';

                    str += '<div style = "text-align: left;padding-left:10px;">' + params.seriesName + ' : ' + util.bigNumFormat(params.value) + '(' + params.percent + '%)</div>';

                    return str;
                }
            },
            legend: {
                orient: 'vertical',
                left: 'left'
            },
            
            
            series: [{
                name: '销售额',
                type: 'pie',
                radius: '55%',
                center: ['50%', '50%'],
                data: [],
                itemStyle: {
                    emphasis: {
                        shadowBlur: 10,
                        shadowOffsetX: 0,
                        shadowColor: 'rgba(0, 0, 0, 0.5)'
                    }
                }
            }]
        };
        // 类目柱状图
        vm.option_for_bar = {
            tooltip: {
                trigger: 'axis',
                axisPointer: { // 坐标轴指示器，坐标轴触发有效
                    type: 'shadow' // 默认为直线，可选为：'line' | 'shadow'
                },
                formatter: function(params) {
                    var str = '<div style = "width:120px;text-align: left;">' + params[0].name + '</div>';

                    for (var i = 0; i < params.length; i++) {
                        str += '<div style = "width:120px;text-align: left;padding-left:10px;">' + params[i].seriesName + ' : ' + util.bigNumFormat(params[i].value) + '</div>';
                    }

                    return str;
                }
            },
            legend: {
                data: []
            },
            grid: {
                left: 60,
                bottom:80
            },
            xAxis: {
                type: 'category',
                data: [],
                axisLabel: {
                    rotate: 37,
                    interval: 0
                }
            },
            yAxis: {
                type: 'value',
                axisLabel: {
                    formatter: function(value, index) {
                        var len = String(value).length;

                        if (value < 10000) {
                            return value;
                        }

                        switch (len) {
                            case 5:
                                return (value / 10000) + '万';
                            case 6:
                                return (value / 10000) + '万';
                            case 7:
                                return (value / 10000) + '万';
                            case 8:
                                return (value / 10000) + '万';
                            case 9:
                                return (value / 100000000) + '亿';
                            case 10:
                                return (value / 100000000) + '亿';
                            case 11:
                                return (value / 100000000) + '亿';
                            case 12:
                                return (value / 100000000) + '亿';
                        }

                        return value;
                    }
                }
            },
            series: [{
                name: '销售额',
                type: 'bar',
                data: []
            }]
        };

        vm.clickEcharts = clickEcharts; //给directive 传 function

    
        function changeType(type) {
            // console.log(type);
            vm.chart.type = type;

            var option = type == 'pie' ? vm.option_for_pie : vm.option_for_bar;
            $rootScope.$emit('echart_draw_EV', option); //通知echarts
        }

        //标题格式化
        function titleFormat() {
            if (vm.catName == '/') {
                vm.chart.title = "全网类目销售占比";
            } else if (vm.catName.indexOf('--') == -1) {
                vm.chart.title = vm.catName + "类目销售占比";
            } else {
                vm.chart.title = vm.catName.split('--')[vm.catName.split('--').length - 1] + "类目销售占比";
            }
        }

        function clickEcharts(params) {


            // console.log('999919911919119');
            // console.log(params);
            
            //饼图
            if(params.seriesType == 'pie'){
                console.log('饼图');
                vm.chart.isLeaf = params.data.isLeaf;
                //click=false,禁止点击
                if(typeof params.data.click !='undefined' && !params.data.click){
                    return;
                }
            }else{ //柱状图
                console.log('柱状图');
                var clickIndex = params.dataIndex;
                // console.log(clickIndex);
                // console.log(vm.option_for_pie.series[0].data);
                // console.log(vm.option_for_pie.series[0].data[clickIndex]);
                var dataParam = vm.option_for_pie.series[0].data[clickIndex];
                vm.chart.isLeaf = dataParam.isLeaf;
                if(typeof dataParam.click !='undefined' && !dataParam.click){
                    return;
                }
            }

            console.log(vm.catName);
            //vm.catName = params
            if (vm.catName == '/') {
                vm.catName = params.name;
            } else {
                vm.catName += '--' + params.name;
            }

            //广播到industry.controller catName
            $rootScope.$emit('industry-catName-change', vm.catName);
            getHTTP();
        }


        function back() {
            vm.chart.isLeaf = false;
            // console.log(vm.catName);
            if (vm.catName.indexOf('--') == -1) { //第一级类目
                vm.catName = vm.chart.root_catName; //重新设置为根类目
                // console.log(vm.catName);
                vm.getHTTP();
            } else { //多级类目
                // console.log("当前所在类目：" + vm.catName);
                var lastSplit = vm.catName.lastIndexOf('--');
                vm.catName = vm.catName.substring(0, lastSplit);

                // console.log("当前所在类目上一级：" + vm.catName);
                vm.getHTTP();
            }

            //广播到industry.controller catName
            $rootScope.$emit('industry-catName-change', vm.catName);

            //

        }

        function backToTop() {
            vm.chart.isLeaf = false;
            vm.catName = vm.chart.root_catName; //重新设置为根类目
            // console.log(vm.catName);
            vm.getHTTP();
            
            //广播到industry.controller catName
            $rootScope.$emit('industry-catName-change', vm.catName);
        }

        function getHTTP() {
            // console.log("getHTTP-param:"+vm.chart.isLeaf);
            // var leaf = vm.chart.isLeaf;
            dashPieService.getHttp(vm.catName, vm.option_for_pie, vm.option_for_bar,vm.chart.isLeaf).then(function() {

                var option = vm.chart.type == 'pie' ? vm.option_for_pie : vm.option_for_bar;

                // console.log(option);
                titleFormat();
                $rootScope.$emit('echart_draw_EV', option); //通知echarts
            });

        }

    }
})();
